using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class GameScript : MonoBehaviour {
	public int NumberOfUnits = 1;
	
	private int[,] _heightMap;
	private List<Unit> _units;
	
	private Unit _selectedUnit;
	
	// Use this for initialization
	void Start () {
		var gridScript = (GridScript)GetComponent("GridScript");
		_heightMap = gridScript.Initialize();
		
		PopulateUnits();
	}
	
	// Update is called once per frame
	void Update () {
		_selectedUnit = GetNextUnit();
		
		if (Input.GetMouseButtonUp(0))
		{
			
		}
	}
	
	private void PopulateUnits()
	{
		_units = new List<Unit>();
		var unit = (GameObject) Resources.Load("Unit");
		
		var width = _heightMap.GetLength(0);
		var length = _heightMap.GetLength(1);
		
		for (var unitCount = 0; unitCount < NumberOfUnits; unitCount++)
		{
			var i = Random.Range(0, width);
			var k = Random.Range(0, length);
			// unit height is 1.5
			var j = _heightMap[i, k] + 1f;
			
			_units.Add((Unit)((GameObject)Instantiate(unit, new Vector3(i, j, k), new Quaternion(0f, 0f, 0f, 0f))).GetComponent("Unit"));
		}
	}
	
	private Unit GetNextUnit()
	{
		return _units[0];
	}
}
